package org.spring.springboot.service.impl;

import org.spring.springboot.dao.UserDao;
import org.spring.springboot.domain.AdminUser;
import org.spring.springboot.service.UserService;
import org.spring.springboot.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 城市业务逻辑实现类
 *
 * Created by 赵新国 on 07/02/2017.
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    /**
     * 获取管理后台用户列表
     *
     * @return
     */
    @Override
    public List<AdminUser> getUserList() {
        return userDao.getUserList();
    }

    /**
     * 查询用户信息
     *
     * @param userId
     * @return
     */
    @Override
    public AdminUser findAdminUser(Short userId) {
        return userDao.selectByPrimaryKey(userId);
    }

    /**
     * 保存/修改用户信息
     *
     * @param adminUser
     */
    @Override
    public void saveUser(AdminUser adminUser) {
        if (adminUser.getUserId() != null) {
            // 修改用户信息
            userDao.updateByPrimaryKeySelective(adminUser);
        } else {
            // 保存用户信息
            adminUser.setPassword(MD5Util.MD5Encode("888888"));
            adminUser.setCreateTime(new Date());
            adminUser.setLastLogin(new Date());
            userDao.insertSelective(adminUser);
        }
    }

    /**
     * 根据用户名查询用户
     *
     * @param userName
     * @return
     */
    @Override
    public AdminUser findByUsername(String userName) {
        return userDao.findByUsername(userName);
    }

    /**
     * 删除用户
     *
     * @param userId
     */
    @Override
    public void deleteUser(Short userId) {
        userDao.deleteByUserId(userId);
    }

    @Override
    public AdminUser login(String userName, String password) {
        AdminUser adminUser = userDao.login(userName, password);
        return adminUser;
    }

}
